var tools = "";

$(function(){
	//初始化检测工具
	getTools();
	var pagePath = $("#pagePath").val();
	$("#search").attr("href","javascript:;");
	//点击查询
	$("#search").click(function(){
		
		var taskId=$("div.dropdown-list[data-name='taskId']").find("a").attr("id");
		
		var taskName = $.trim($("#taskName").val());
		var lineId = $("#lineDiv").children().attr("id");
		var begTime = $("#begTime").val();
		var endTime = $("#endTime").val();
		var segmentId = $("#segmentDiv").children().attr("id");
		if("" == taskId || "jcrwmc" == taskId || undefined == taskId){
			taskId = "";
		}
		if("" == lineId || "gdmc" == lineId || undefined == lineId){
			lineId = "";
		}
		if("" == segmentId || "jcgdmc" == segmentId || undefined == segmentId){
			segmentId = "";
		}
		$("#taskNameHiddenId").val(taskId);
		$("#lineHiddenId").val(lineId);
		$("#begTimeHiddenId").val(begTime);
		$("#endTimeHiddenId").val(endTime); 
		$("#segmentHiddenId").val(segmentId);
		
		var data={"taskName":taskName,"lineId":lineId,"begTime":begTime,"endTime":endTime,"segmentId":segmentId}
		var map={"fillDiv":"#searchBody",
	              "url":""+pagePath+"/insp/inspTaskManager/findByCondition.json"};
	    searchPage(map,data);
	});
	
	//列表全选、全不选
    $("input[name=parentSelect1]").live("click",function(){
        var booleanParentSelect=$("input[name=parentSelect1]").prop("checked");
        $("input[name=childSelect1]").prop("checked", booleanParentSelect);
    });
	
  //新增mark点 全选、全不选
    $("input[name=parentSelectAdd]").live("click",function(){
        var booleanParentSelect=$("input[name=parentSelectAdd]").prop("checked");
        $("input[name=childSelectAdd]").prop("checked", booleanParentSelect);
    });
	
  //修改mark点 全选、全不选
    $("input[name=parentSelectMod]").live("click",function(){
        var booleanParentSelect=$("input[name=parentSelectMod]").prop("checked");
        $("input[name=childSelectMod]").prop("checked", booleanParentSelect);
    });
    
	/* 日期格式化 */
	Date.prototype.Format = function(fmt) {
        var o = {
            "M+" : this.getMonth() + 1, //月份
            "d+" : this.getDate(), //日
            "h+" : this.getHours(), //小时
            "m+" : this.getMinutes(), //分
            "s+" : this.getSeconds(), //秒
            "q+" : Math.floor((this.getMonth() + 3) / 3), //季度
            "S" : this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
                    .substr(4 - RegExp.$1.length));
        for ( var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1,
                        (RegExp.$1.length == 1) ? (o[k])
                                : (("00" + o[k])
                                .substr(("" + o[k]).length)));
        return fmt;
    }
});

/**
 * 分页
 * @param current
 * @param pageCount
 * @param totalSize
 */
function commonPages(map){
    $("#commonsPage").createPage({
        current:parseInt(map.current),
        pageCount:parseInt(map.pageCount),
        totalSize:parseInt(map.totalSize),
        url:map.url,
        fillDiv:map.fillDiv,
        backFn:function(current,pageCount,totalSize,url,fillDiv){
            var resultMap={"current":parseInt(current),"isPage":"isPage","url":url,"fillDiv":fillDiv};
            var data={};
            searchPage(resultMap,data);
        }
    })
}

function searchPage(map,data){
	//获取隐藏域的值
	var taskName = $("#taskNameHiddenId").val();
	var lineId = $("#lineHiddenId").val();
	var begTime = $("#begTimeHiddenId").val();
	var endTime = $("#endTimeHiddenId").val();
	var segmentId = $("#segmentHiddenId").val();
	
	if(lineId == "gdmc" || lineId == ""){
		lineId = "";
	}
	if(segmentId == "jcgdmc" || segmentId == ""){
		segmentId = "";
	}
    var isPage=map.isPage;
    var current=1;
    if("isPage"==isPage){
        current=parseInt(map.current)
    }else{
        current=1;
    }
    var pageCount=$("#pageSelect").val();
    data.pageCount=parseInt(pageCount);
    data.current=current;
    //参数注入
    data.taskId = taskName;
    data.lineId = lineId;
    data.begTime = begTime;
    data.endTime = endTime;
    data.segmentId = segmentId;
    
    $.ajax({
        url:map.url,
        type:"post",
        dataType:"json",
        data:data,
        success:function(d){
            var p= d.main;
            if(p.length==0){
            	//如果没有数据，添加图片隐藏分页
            	$("#searchBody").empty();
            	$("#noDataImg").removeClass('none')
            	$("#commonsPage").addClass('none')
            }else{
            	$("#noDataImg").addClass('none')
            	$("#commonsPage").removeClass('none')
            var html='';
            for(var i=0;i< p.length;i++){
                html+=createProHtml(p[i],i);
            }
            $(map.fillDiv).html(html);
            if("isPage"==isPage){
                $("#totalSize").html(d.pageTotal);
            }else{
                $("#commonsPage").unbind();
                var resultMap={"current":1,"pageCount":pageCount,"totalSize":d.pageTotal,"url":map.url,"fillDiv":map.fillDiv};
                commonPages(resultMap);
            }
        }}
    });
}



//构建查询结果
function createProHtml(ps,i){
    var resourcePath = $("#resourcePath").val();
	var html = "";
	var hangClass = 'ul-header bg-white';
    if(i%2 != 0){
        hangClass = 'ul-header bg-second';
    }
    var id = ps.ID;
    var taskName = (null != ps.TASKNAME && "" != $.trim(ps.TASKNAME) ? ps.TASKNAME : '-');
    var lineName = (null != ps.LINENAME && "" != $.trim(ps.LINENAME) ? ps.LINENAME : '-');
    var fromPig = (null != ps.FROMPIG && "" != $.trim(ps.FROMPIG) ? ps.FROMPIG : '-');
    var toPig = (null != ps.TOPIG && "" != $.trim(ps.TOPIG) ? ps.TOPIG : '-');	
    var createDate = (null != ps.CREATEDATE && "" != $.trim(ps.CREATEDATE) ? new Date(ps.CREATEDATE).Format("yyyy-MM-dd") : '-');
    var createUser = (null != ps.CREATEUSER && "" != $.trim(ps.CREATEUSER) ? ps.CREATEUSER : '-');	
    var stausNum = 	ps.STAUS;
    var staus;
    if(stausNum =="1"){
    	staus = "未发布";
    }else if(stausNum =="2"){
    	staus = "已发布";
    }else if(stausNum =="3"){
    	staus = "进行中";
    }else if(stausNum =="4"){
    	staus = "已完成";
    }else{
    	staus = "-";
    }
    
	html += "<div class='info-tr'>" +
			"<ul class='"+ hangClass +"'>" +
				"<li class='w5'><span class='Check-Box'><input id="+ id+" class="+id+" type='checkbox' name='childSelect'/></span></li>" +
				"<li class='w20'>"+taskName +"</li>" +
				"<li class='w10'>"+lineName +"</li>" +
				"<li class='w10'>"+fromPig +"</li>" +
				"<li class='w10'>"+toPig+"</li>" +
				"<li class='w10'>"+createDate+"</li>" +
				"<li class='w10'>"+createUser+"</li>" +
				"<li class='w10'>"+ staus +"</li>" +
				"<li class='w15 no-right'>" +
					"<a href='javascript:;' onclick='deleteFun(\""+id+"\");' class='delet'><img src='"+resourcePath+"/images/comm/del.png' alt=''/></a>" +
					"<a href='javascript:;' onclick='publishFun(\""+id+"\");' class='public'><img src='"+resourcePath+"/images/comm/icon1_07.png' alt=''/></a>" +
				"</li>" +
			"</ul>" +
		 "</div>";
	return html;
}
//删除
function deleteFun(ids){
	var pagePath = $("#pagePath").val();
	//获取所有name = childSelect的input
	var childBox = $("input[name='childSelect']");
	if(ids == "mutiCheck"){
		var idsArr = new Array();
		var count = 0;
		for(var i= 0 ; i < childBox.length;i ++){
			if(childBox[i].checked){
				idsArr.push(childBox[i].id);
				count++;
			}
		}
		if(idsArr.length < 1){
			alert("请先勾选需要删除的任务");
			return ;
		}
		var idStr = idsArr.join();
		var delFlag = window.confirm("确定要删除吗");
		if(delFlag == true){
			var url = pagePath + "/insp/inspTaskManager/deleteFun.json?idStr="+idStr;
			$.ajax({
				url:url,
				type:'post',
				dataType:'json',
				success:function(data){
					var count = data.count;
					alert("共删除了【 "+count +" 】条数据");
					window.location.href = pagePath + "/insp/inspTaskManager/getInspTaskManagerIndex.html";
				}
			});
			
		}
	}else{
		//获取全选checkbox，并设置不选择
		$("input[name='childSelect']").prop("checked",false);
		//设置列表中checkbox为不勾选
		childBox.prop("checked",false);
		//勾选上当前需要删除的数据
		$("."+ids).prop("checked",true);
		var delFlag = window.confirm("确定要删除吗");
		if(delFlag == true){
			var url = pagePath + "/insp/inspTaskManager/deleteFun.json?idStr="+ids;
			$.ajax({
				url:url,
				type:'post',
				dataType:'json',
				success:function(data){
					if(data.count == 0){
						alert("该记录已经不允许删除");
						return ;
					}else{
						alert("删除成功");
						window.location.href = pagePath + "/insp/inspTaskManager/getInspTaskManagerIndex.html";
					}
				}
			});
		}
	}
}

//发布
function publishFun(ids){
	var pagePath = $("#pagePath").val();
	var childBox = $("input[name='childSelect']");
	if(ids == "mutiCheck"){
		var idsArr = new Array();
		for(var i= 0 ; i< childBox.length ;i ++){
			if(childBox[i].checked){
				idsArr.push(childBox[i].id);
			}
		}
		if(idsArr.length < 1){
			alert("请先勾选需要发布的任务");
			return ;
		}
		var idStr = idsArr.join();
		var delFlag = window.confirm("确定要发布吗?");
		if(delFlag == true){
			var url = pagePath + "/insp/inspTaskManager/publishFun.json?idStr="+idStr;
			$.ajax({
				url:url,
				type:'post',
				dataType:'json',
				success:function(data){
					var count = data.count;
					alert("成功发布【"+ count +"】条任务");
					
					window.location.href = pagePath + "/insp/inspTaskManager/getInspTaskManagerIndex.html";
				}
			});
		}
	}else{
		$("input[name='parentSelect']").prop("checked",false);
		childBox.prop("checked",false);
		$("."+ids).prop("checked",true);
		var delFlag = window.confirm("确定要发布吗?");
		if(delFlag == true){
			var url = pagePath + "/insp/inspTaskManager/publishFun.json?idStr="+ids;
			$.ajax({
				url:url,
				type:'post',
				dataType:'json',
				success:function(data){
//					var count = data.count;
					alert("发布成功");
					$("input:checked").parent().parent().parent().find("li:eq(7)").html("已发布");
				}
			});
		}
	}
}

//获取检测管段
function getInspSegment(lineId){
	$("#inspSegment").html('');
	
	var pagePath = $("#pagePath").val();
	var url = pagePath + "/insp/inspTaskManager/getInspSegment.json";
	$.ajax({
		url:url,
		type:'post',
		dataType:'json',
		data:{"lineId":lineId},
		success:function(d){
			var inspSegment = d.segment;
			var html='';
			$.each(inspSegment,function(i,evalue){
				html += "<li><a href='javascript:void(0);' id='"+ evalue.ID+"'>"+ evalue.NAME+"</a></li>";
			});
			$("#segmentDiv").html("<a href='javascript:void(0);' id='jcgdmc'>检测管段名称</a><span></span>");
			$("#inspSegment").html(html);
			$("#inspSegment a").click(function(){
				//查询检测任务
				resultAjax(getJsPath()+"../insp/metalLoss/task.json","taskSegmentId="+$(this).attr("id"),parseTask,"JSON");
				
			});
			resetTask();
		}
	});
}


//重置task 选项
function resetTask()
{
	var baseSelect = $(".selectTask").parent().find(".drop-list-min a");
	baseSelect.html("检测任务名称");
	baseSelect.removeAttr("id");
	$(".selectTask").empty();
}


//点击新增
function getAdd(){
	//构建跟踪点列表头
	var html="";
	$("#pointDiv").html('');
	html += "<div class='Popup_table'>" +
	"<div class='info-header clearFix'>" +
	"<ul class='ul-header'>" +
	"<li class='w5'>" +
	"<span class='Check-Box1 allBox'><input type='checkbox' name='parentSelectAdd' /></span>" +
	"</li>" +
	"<li class='w150'>MARK点名称</li>" +
	"<li class='w150'>MARK点编号</li>" +
	"<li class='w150'>最近参考点</li>" +
	"</ul>";
	html+="</div></div>";
	$("#pointDiv").html(html);
	//显示新增弹窗
	$("#new_add").modal("show");
}

//新增时 管道和管段的联动
function getAddInspSegment(lineId){
	$("#addInspSegment").html('');
	//先对mark点做一次移除
	var html="";
	$("#pointDiv").html('');
	html += "<div class='Popup_table'>" +
	"<div class='info-header clearFix'>" +
	"<ul class='ul-header'>" +
	"<li class='w5'>" +
	"<span class='Check-Box1 allBox'><input type='checkbox' name='parentSelectAdd' /></span>" +
	"</li>" +
	"<li class='w150'>MARK点名称</li>" +
	"<li class='w150'>MARK点编号</li>" +
	"<li class='w150'>最近参考点</li>" +
	"</ul>";
	html+="</div></div>";
	$("#pointDiv").html(html);
	var pagePath = $("#pagePath").val();
	var url = pagePath + "/insp/inspTaskManager/getInspSegment.json";
	$.ajax({
		url:url,
		type:'post',
		dataType:'json',
		data:{"lineId":lineId},
		success:function(d){
			var inspSegment = d.segment;
			var html='';
			$.each(inspSegment,function(i,evalue){
				html += "<li onclick='getInspPoint(\""+evalue.ID+"\");'><a href='javascript:void(0);' id='"+evalue.ID+"'>"+evalue.NAME+"</a></li>";
			});
			$("#inspSegmentDiv").html("<a href='javascript:void(0);'>检测管段名称</a><span></span>");
			$("#addInspSegment").html(html);
		}
	});
}

//获取新增时跟踪点
function getInspPoint(inspSegmentId){
	var pagePath = $("#pagePath").val();
	var hangClass = '';
	if(inspSegmentId == "" ){
		alert("请选择检测管段");
	}else{
		var url = pagePath + "/insp/inspTaskManager/getMarkPoint.json";
		$.ajax({
			url:url,
			type:'post',
			dataType:'json',
			data:{"inspSegmentId":inspSegmentId},
			success:function(data){
				var pig = data.pig;
				$("#getFromPig").val(pig.fromPig);
				$("#getToPig").val(pig.toPig);
				
				var html = "";
				$("#pointDiv").html('');
				var dataList = data.markPoint;
				html += "<div class='Popup_table'>" +
				"<div class='info-header clearFix'>" +
				"<ul class='ul-header'>" +
				"<li class='w5'>" +
				"<span class='Check-Box1 allBox'><input type='checkbox' name='parentSelectAdd' /></span>" +
				"</li>" +
				"<li class='w150'>MARK点名称</li>" +
				"<li class='w150'>MARK点编号</li>" +
				"<li class='w150'>最近参考点</li>" +
				"</ul>";
				
				$.each(dataList,function(i,evalue){
					 if(i%2 != 0){
					     hangClass = 'ul-header bg-second';
					 }else{
						 hangClass = 'ul-header bg-white';
					 }
					 html += "<div class='info-tr'>" +
					 	"<ul class='"+hangClass+"'>" +
					 		"<li class='w5'><span class='Check-Box1'><input type='checkbox' id="+evalue.ID+" name='childSelectAdd'/></span></li>" +
					 		//mark编号
					 		"<li class='w150'>"+((evalue.CODE == null || evalue.CODE == '') ? '-':evalue.CODE)+"</li>" +
					 		//mark名称
					 		"<li class='w150'>"+evalue.NAME+"</li>" +
					 		//参考点
					 		"<li class='w150'>"+evalue.REFERENCE+"</li>" +
					 	"</ul>" +
					 "</div>";
				});
				html+="</div></div>";
				$("#pointDiv").html(html);
			}
		});
	}
}

//获取检测工具
function getTools(){
	var pagePath = $("#pagePath").val();
	var url = pagePath + "/insp/inspTaskManager/getTools.json";
	$("#inspToolDiv").html('');
	$.ajax({
		url:url,
		type:'post',
		dataType:'json',
		success:function(data){
			var tools = data.tools;
			var html = "";
			$.each(tools,function(i,evalue){
				html += "<span class='ck' >" +
						"<span class='check-box selected' onclick=''><i></i></span>" +
						"<a class='check-box-name' href='javascript:void(0);' onclick=''>"+evalue.NAME+"</a>" +
						"</span>";
			});
			$("#inspToolDiv").html(html);	
		}
	});
}

//保存检测任务
function saveTask(){
	var inspTaskName = $("#inspTaskName").val();
	var inspLineId = $("#inspLineDiv").children().attr("id");
	var inspSegmentId = $("#inspSegmentDiv").children().attr("id");
	var insptoolBox = $(".ck").children("span").children();
	var inspChildBox = $("input[name='childSelectAdd']");
	
	var fromPig = $("#getFromPig").val();
	var toPig = $("#getToPig").val();
	//判断是否有勾选mark点
	var checkFlag = false;
	if("" == $.trim(inspTaskName)){
		alert("请输入检测任务名称");
		return;
	}
	if("" == inspLineId || undefined == inspLineId ){
		alert("请选择管道名称");
		return;
	}
	if("" == inspSegmentId || undefined == inspSegmentId){
		alert("请选择检测管段名");
		return;
	}
	if(!insptoolBox.hasClass("active")){
		alert("请勾选检测工具");
		return;
	}else{
		var inspToolArr = new Array();
		var inspToolStr = "";
		for(var i= 0 ; i < insptoolBox.length;i ++){
			if(insptoolBox[i].hasAttribute("class")){
				var toolName = $(".check-box-name")[i];
				inspToolArr.push(toolName.innerText);
			}
		}
		if(inspToolArr.length < 1){
			alert("请先勾选检测工具");
			return ;
		}
		inspToolStr = inspToolArr.join();
	}
	
	if(inspChildBox.length < 1){
		alert("该检测管段没有对应的mark点");
		return;
	}else {
		var markPointArr = new Array();
		var markPointStr = "";
		for(var i= 0 ; i < inspChildBox.length;i ++){
			if(inspChildBox[i].checked){
				markPointArr.push(inspChildBox[i].id);
				checkFlag = true;
			}
		}
		markPointStr = markPointArr.join();
		if(markPointStr.length < 1){
			alert("请先勾选mark点");
			return ;
		}
	}
	if(checkFlag){
		var pagePath = $("#pagePath").val();
		var url = pagePath + "/insp/inspTaskManager/saveTask.json";
		$.ajax({
			url:url,
			type:'post',
			dataType:'json',
			data:{"taskName":inspTaskName,"lineId":inspLineId,"segmentId":inspSegmentId,"markPoint":markPointStr,"testTool":inspToolStr,"fromPig":fromPig,"toPig":toPig},
			success:function(data){
				if(data.msgCode == 1){
					alert("请输入检测任务名称");
				}else if(data.msgCode == 2){
					alert("请选择管道名称");
				}else if(data.msgCode == 3){
					alert("请选择检测管段名称");
				}else if(data.msgCode == 4){
					alert("请勾选检测工具");
				}else if(data.msgCode == 5){
					alert("请勾选mark点");
				}else if(data.msgCode == "done"){
					alert("操作成功");
					window.location.href = pagePath + "/insp/inspTaskManager/getInspTaskManagerIndex.html";
					$("#new_add").modal("hide");
				}
			}
		});
	}
}

//点击修改
function getModify(){
	var pagePath = $("#pagePath").val();
	var childBox = $("input[name='childSelect']");
	var count = 0;
	var dataId = "";
	//未发布状态
	var status = false;
	for(var i= 0 ; i < childBox.length;i ++){
		if(childBox[i].checked){
			if(count == 0){
//				dataId = childBox[i].attr("id");
				dataId = childBox[i].attributes.id.value;
				//判断勾选的这条任务是否未发布，否则不允许修改
				var url0 = pagePath + "/insp/inspTaskManager/getTask.json";
				$.ajax({
					url:url0,
					type:'post',
					dataType:'json',
					data:{"taskId":dataId},
					async: false,
					success:function(data){
						var st = data.task.staus;
						if(st != 1){
							status = true;
						}
					}
				});
			}
			count++;
		}
	}
	if(status){
		alert("该任务不能修改");
		$("#new_edit").modal("hide");
		return;
	}
	if(count != 1){
		alert("请勾选一条数据进行操作");
		$("#new_edit").modal("hide");
		return;
	}else{
		var url = pagePath + "/insp/inspTaskManager/getInspTaskById.json";
		$.ajax({
			url:url,
			type:'post',
			dataType:'json',
			data:{"taskId":dataId},
			async: false,
			success:function(data){
				//取得任务
				var task = data.modTask;
				$("#modTaskId").val(task.id);
				$("#modTaskName").val(task.name);
				
				//取得管道
				var pipeLine = data.modPipeLine;
				$("#modPipeLineDiv").html('');
				$("#modPipeLineDiv").html("<a href='javascript:void(0);' id='"+pipeLine.id+"'>"+pipeLine.name +"</a><span></span>");
				var pipeLineList = data.modAllLine;
				var modPipeLineAllDivHtml = "";
				$("#modPipeLineAllDiv").html("");
				$.each(pipeLineList,function(i,evalue){
					modPipeLineAllDivHtml += "<li onclick='getModInspSegment(\""+evalue.id +"\")' ><a href='javascript:void(0);' id='"+evalue.id+"'>"+evalue.name+"</a></li>";
				});
				$("#modPipeLineAllDiv").html(modPipeLineAllDivHtml);
				
				//取得检测管段
				var inspSegment = data.modInspSegment;
				$("#modInspSegmentDiv").html("");
				$("#modInspSegmentDiv").html("<a href='javascript:void(0);' id='"+inspSegment.id+"'>"+inspSegment.inspSegmentName +"</a><span></span>");
				var inspSegmentList = data.modAllInspSegment;
				var modInspSegmentAllDivHtml="";
				$("#modInspSegmentAllDiv").html("");
				$.each(inspSegmentList,function(i,evalue){
					modInspSegmentAllDivHtml += "<li onclick='getModInspPoint(\""+evalue.ID +"\")' ><a href='javascript:void(0);' id='"+evalue.ID+"'>"+evalue.INSPSEGMENTNAME+"</a></li>";
				});
				$("#modInspSegmentAllDiv").html(modInspSegmentAllDivHtml);
				
				//取得检测工具
				var modTool = data.modTool;
				var modAllTool = data.modAllTool;
				var modToolDivHtml = "";
				$("#modToolDiv").html("");
				$.each(modAllTool,function(i,evalue){
					modToolDivHtml += "<span class='ck'>" +
							"<span class='check-box selected' onclick=''>";
							if(modTool.includes(evalue.NAME)){
								modToolDivHtml +="<i class='active'></i>";
							}else{
								modToolDivHtml +="<i></i>";
							}
							modToolDivHtml +="</span>" +
									"<a class='check-box-name' href='javascript:void(0);' onclick=''>"+evalue.NAME+"</a></span>";
				});
				$("#modToolDiv").html(modToolDivHtml);
				
				//获取跟踪点
				var modPoint = data.modPoint;
				var modAllPointList = data.modAllPoint;
				var modTrackPointDivHtml = "";
				$("#modTrackPointDiv").html("");
				var hangClass = "";
				modTrackPointDivHtml += "<div class='Popup_table'>" +
					"<div class='info-header clearFix'>" +
					"<ul class='ul-header'>" +
					"<li class='w5'>" +
					"<span class='Check-Box1 allBox'><input type='checkbox' name='parentSelectMod' /></span>" +
					"</li>" +
					"<li class='w150'>MARK点名称</li>" +
					"<li class='w150'>MARK点编号</li>" +
					"<li class='w150'>最近参考点</li>" +
					"</ul>";
				
				$.each(modAllPointList,function(i,evalue){
					 var chk ;
					 if(i%2 != 0){
					     hangClass = 'ul-header bg-second';
					 }else{
						 hangClass = 'ul-header bg-white';
					 }
					 if(modPoint.includes(evalue.ID)){
						 chk = "checked";
					 }else{
						 chk ="";
					 }
					modTrackPointDivHtml +="<div class='info-tr'>" +
							"<ul class='"+hangClass+"'>" +
							"<li class='w5'><span class='Check-Box1'><input id="+evalue.ID+" type='checkbox' "+chk+" name='childSelectMod'/></span></li>" +
							"<li class='w150'>"+((evalue.CODE == null || evalue.CODE == '') ? '-':evalue.CODE)+"</li>" +
							"<li class='w150'>"+evalue.NAME+"</li>" +
							"<li class='w150'>"+evalue.REFERENCE+"</li>" +
							"</ul>" +
							"</div>";
				});
				modTrackPointDivHtml+="</div></div>";
				$("#modTrackPointDiv").html(modTrackPointDivHtml);
				
				//显示修改弹窗
				$("#new_edit").modal("show");
			}
		});
	}
}	
//保存修改检测任务
function saveModTask(){
	var taskId = $("#modTaskId").val();
	var inspTaskName = $("#modTaskName").val();
	var inspLineId = $("#modPipeLineDiv").children().attr("id");
	var inspSegmentId = $("#modInspSegmentDiv").children().attr("id");
	var insptoolBox = $("#modToolDiv").children().children("span").children();
//	var inspChildBox = $("#modTrackPointDiv").find("input[name='childSelect1']");
	var inspChildBox = $("input[name='childSelectMod']");
	//判断是否有勾选mark点
	var checkFlag = false;
	if("" == $.trim(inspTaskName)){
		alert("请输入检测任务名称");
		return;
	}
	if("" == inspLineId || undefined == inspLineId ){
		alert("请选择管道名称");
		return;
	}
	if("" == inspSegmentId || undefined == inspSegmentId){
		alert("请选择检测管段名");
		return;
	}
	if(!insptoolBox.hasClass("active")){
		alert("请勾选检测工具");
		return;
	}else{
		var inspToolArr = new Array();
		var inspToolStr = "";
		for(var i= 0 ; i < insptoolBox.length;i ++){
			if(insptoolBox[i].className != ""){
				var toolName = $("#modToolDiv").children().children("a")[i];
				inspToolArr.push(toolName.innerText);
			}
		}
		inspToolStr = inspToolArr.join();
		if(inspToolArr.length < 1){
			alert("请先勾选检测工具");
			return ;
		}
	}
	
	if(inspChildBox.length < 1){
		alert("该检测管段没有对应的mark点");
		return;
	}else {
		var markPointArr = new Array();
		var markPointStr = "";
		for(var i= 0 ; i < inspChildBox.length;i ++){
			if(inspChildBox[i].checked){
				markPointArr.push(inspChildBox[i].id);
				checkFlag = true;
			}
		}
		markPointStr = markPointArr.join();
		if(markPointStr.length < 1){
			alert("请先勾选mark点");
			return ;
		}
	}
	if(checkFlag){
		var pagePath = $("#pagePath").val();
		var url = pagePath + "/insp/inspTaskManager/saveModTask.json";
		$.ajax({
			url:url,
			type:'post',
			dataType:'json',
			data:{"taskId":taskId,"taskName":inspTaskName,"lineId":inspLineId,"segmentId":inspSegmentId,"markPoint":markPointStr,"testTool":inspToolStr},
			success:function(data){
				if(data.msgCode == 1){
					alert("请输入检测任务名称");
				}else if(data.msgCode == 2){
					alert("请选择管道名称");
				}else if(data.msgCode == 3){
					alert("请选择检测管段名称");
				}else if(data.msgCode == 4){
					alert("请勾选检测工具");
				}else if(data.msgCode == 5){
					alert("请勾选mark点");
				}else if(data.msgCode == "done"){
					alert("操作成功");
					$("#new_edit").modal("hide");
					window.location.href = pagePath + "/insp/inspTaskManager/getInspTaskManagerIndex.html";
					}
				}
			});
		}
	}


function getModInspSegment(lineId){
	$("#modInspSegmentAllDiv").html('');
	var html="";
	$("#modTrackPointDiv").html('');
	html += "<div class='Popup_table'>" +
	"<div class='info-header clearFix'>" +
	"<ul class='ul-header'>" +
	"<li class='w5'>" +
	"<span class='Check-Box1 allBox'><input type='checkbox' name='parentSelectMod' /></span>" +
	"</li>" +
	"<li class='w150'>MARK点名称</li>" +
	"<li class='w150'>MARK点编号</li>" +
	"<li class='w150'>最近参考点</li>" +
	"</ul>";
	html+="</div></div>";
	$("#modTrackPointDiv").html(html);
	var pagePath = $("#pagePath").val();
	var url = pagePath + "/insp/inspTaskManager/getInspSegment.json";
	$.ajax({
		url:url,
		type:'post',
		dataType:'json',
		data:{"lineId":lineId},
		success:function(d){
			var inspSegment = d.segment;
			var html='';
			$.each(inspSegment,function(i,evalue){
				html += "<li onclick='getModInspPoint(\""+evalue.ID+"\");'><a href='javascript:void(0);' id='"+evalue.ID+"'>"+evalue.NAME+"</a></li>";
			});
			$("#modInspSegmentDiv").html("<a href='javascript:void(0);'>检测管段名称</a><span></span>");
			$("#modInspSegmentAllDiv").html(html);
		}
	});
}

//获取修改时的mark点
function getModInspPoint(inspSegmentId){

	var pagePath = $("#pagePath").val();
	var hangClass = '';
	if(inspSegmentId == "" ){
		alert("请选择检测管段");
	}else{
		var url = pagePath + "/insp/inspTaskManager/getMarkPoint.json";
		$.ajax({
			url:url,
			type:'post',
			dataType:'json',
			data:{"inspSegmentId":inspSegmentId},
			success:function(data){
				var html = "";
				$("#modTrackPointDiv").html('');
				var dataList = data.markPoint;
				html += "<div class='Popup_table'>" +
						"<div class='info-header clearFix'>" +
						"<ul class='ul-header'>" +
						"<li class='w5'>" +
						"<span class='Check-Box1 allBox'><input type='checkbox' name='parentSelectMod' /></span>" +
						"</li>" +
						"<li class='w150'>MARK点名称</li>" +
						"<li class='w150'>MARK点编号</li>" +
						"<li class='w150'>最近参考点</li>" +
						"</ul>";
				
				$.each(dataList,function(i,evalue){
					 if(i%2 != 0){
					     hangClass = 'ul-header bg-second';
					 }else{
						 hangClass = 'ul-header bg-white';
					 }
					 html +="<div class='info-tr'>" +
						"<ul class='"+hangClass+"'>" +
						"<li class='w5'><span class='Check-Box1'><input id='"+evalue.ID+"' type='checkbox' name='childSelectMod'/></span></li>" +
						"<li class='w150'>"+((evalue.CODE == null || evalue.CODE == '') ? '-':evalue.CODE)+"</li>" +
						"<li class='w150'>"+evalue.NAME+"</li>" +
						"<li class='w150'>"+evalue.REFERENCE+"</li>" +
						"</ul>" +
						"</div>";
				});
				html+="</div></div>";
				$("#modTrackPointDiv").html(html);
			}
		});
	}
}

function addClose(){
	/*$("#inspTaskName").val("");
	$("#inspLineDiv").html("<a href='javascript:void(0);'>管道名称</a><span></span>");
	$("#inspSegmentDiv").html("<a href='javascript:void(0);'>检测管段名称</a><span></span>");
	$("#pointDiv").html("");*/
	var pagePath = $("#pagePath").val();
	window.location.href = pagePath + "/insp/inspTaskManager/getInspTaskManagerIndex.html";
}


/**
 * input控制键盘输入
 * 
 * @author TKC 
 * @date 2015-11-02
 * @parameter e:event,inputPra: 传入input的id
 */
function noPermitInput(e,inputPra){    
   var evt = window.event || e ;  
    if(isIE()){  
        evt.returnValue=false; //ie 禁止键盘输入  
        $("#"+inputPra).val("");
    }else{  
        evt.preventDefault(); //fire fox 禁止键盘输入  
        $("#"+inputPra).val("");
    }     
}  
function isIE() {  
if (window.navigator.userAgent.toLowerCase().indexOf("msie") >= 1)  
    return true;  
else  
    return false;  
} 

function triggerLaydate(timeType){
	if(timeType == 'begTime'){
		$("#begTime").focus();
		$("#begTime").trigger('onclick');
	}
	if(timeType == 'endTime'){
		$("#endTime").focus();
		$("#endTime").trigger('onclick');
	}
}